package com.hzy.admin.configs;

import com.hzy.admin.filter.FilterHandlerInterceptor;
import com.hzy.admin.services.system.ISysAppLogService;
import com.hzy.admin.services.system.ISysUserService;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>
 * 请求 过滤器 配置
 * </p>
 *
 * @author hzy
 * @since 2020-04-26
 */
@Configuration
public class HzyWebMvcConfigurer implements WebMvcConfigurer {

    private final ISysUserService sysUserService;
    private final ISysAppLogService sysAppLogService;
    private final AppConfigs appConfigs;

    public HzyWebMvcConfigurer(ISysUserService sysUserService, ISysAppLogService sysAppLogService,
            AppConfigs appConfigs) {
        this.sysUserService = sysUserService;
        this.sysAppLogService = sysAppLogService;
        this.appConfigs = appConfigs;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new FilterHandlerInterceptor(sysUserService, sysAppLogService, appConfigs))
                .addPathPatterns("/**");
    }

    /**
     * 解决跨域问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE").allowCredentials(true)
                .maxAge(3600).allowedHeaders("*");
    }

    // @Override
    // public void addViewControllers(ViewControllerRegistry registry) {
    // registry.addViewController("/")
    // .setViewName("admin");
    // registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    // }
}
